package com.yelin.taketitle

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.webkit.ConsoleMessage
import android.webkit.WebChromeClient
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.github.lzyzsd.jsbridge.DefaultHandler
import com.google.gson.Gson
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.activity_search_reslit.*

class SearchResultActivity : AppCompatActivity() {
    lateinit var resultBean: ResultBean
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search_reslit)
        resultBean = intent.getSerializableExtra("ResultBean") as ResultBean

        webView.setDefaultHandler(DefaultHandler())
        webView.setWebChromeClient(object : WebChromeClient() {
            override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {
                Log.d("jsBridge log", consoleMessage.message())
                return super.onConsoleMessage(consoleMessage)
            }
        })
        webView.loadUrl("file:///android_asset/dist/index.html")

        if (resultBean.questions.isNotEmpty()){
            val json = Gson().toJson(resultBean.questions[0])
            Logger.d(json)
            sendDataToJS(json)
        }
    }


    private fun sendDataToJS(json: String) {
        webView.callHandler(
            "getQuestionData", json
        ) { data ->
            Toast.makeText(
                this@SearchResultActivity,
                data,
                Toast.LENGTH_SHORT
            ).show()
        }
    }

/*    fun test(){
        //第一次设置缓存位置
//        RichText.initCacheDir(this)
        tv_src_title.text = "原题识别："+resultBean.text
        if (resultBean.questions.isNotEmpty()) {
            var questions = resultBean.questions[0]
            tv_subject.text = "科目:"+questions.subject
            RichText.from(questions.content)
                .into(tv_title)

            RichText.from(questions.analysis)
                .into(tv_analysis)

            tv_answer.text = "答案："+questions.answer
        }
    }*/
}
